package site.marqstree.smartbutler.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import site.marqstree.smartbutler.R;
import site.marqstree.smartbutler.entity.User;

/*
 * 项目名: SmartButler
 * 包名: site.marqstree.smartbutler.ui
 * 文件名: RegisteredActivity
 * 创建者: marqstree
 * 创建时间: 2019/12/31 13:48
 * 描述: TODO
 */
public class RegisteredActivity extends BaseActivity {

    @BindView(R.id.et_user)
    EditText mEtUser;
    @BindView(R.id.et_age)
    EditText mEtAge;
    @BindView(R.id.et_desc)
    EditText mEtDesc;
    @BindView(R.id.rb_boy)
    RadioButton mRbBoy;
    @BindView(R.id.rb_girl)
    RadioButton mRbGirl;
    @BindView(R.id.radioGroup)
    RadioGroup mRadioGroup;
    @BindView(R.id.et_pass)
    EditText mEtPass;
    @BindView(R.id.et_password)
    EditText mEtPassword;
    @BindView(R.id.et_email)
    EditText mEtEmail;
    @BindView(R.id.btnRegistered)
    Button mBtnRegistered;

    //性别
    //true:男
    private boolean mGender = true;
    //简介
    private String mDesc = "";
    private String mName = "";
    private String mAge = "";
    private String mDesc1 ="";
    private String mPass="";
    private String mPassword="";
    private String mEmail="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btnRegistered,R.id.rb_boy, R.id.rb_girl})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnRegistered:
                //获取到输入框的值
                mName = mEtUser.getText().toString().trim();
                mAge = mEtAge.getText().toString().trim();
                mDesc = mEtDesc.getText().toString().trim();
                mPass = mEtPass.getText().toString().trim();
                mPassword = mEtPassword.getText().toString().trim();
                mEmail = mEtEmail.getText().toString().trim();

                //判断是否为空
                if ( TextUtils.isEmpty(mName) ||
                        TextUtils.isEmpty(mAge) ||
                        TextUtils.isEmpty(mPass) ||
                        TextUtils.isEmpty(mPassword) ||
                        TextUtils.isEmpty(mEmail)) {
                    Toast.makeText(this, getString(R.string.text_tost_empty), Toast.LENGTH_SHORT).show();
                    return;
                }

                //判断两次输入的密码是否一致
                if (!mPass.equals(mPassword)) {
                    Toast.makeText(this, R.string.text_two_input_not_consistent, Toast.LENGTH_SHORT).show();
                    return;
                }

                //判断简介是否为空
                if (TextUtils.isEmpty(mDesc)) {
                    mDesc1 = getString(R.string.text_nothing);
                }

                //注册
                User user = new User();
                user.setUsername(mName);
                user.setPassword(mPassword);
                user.setEmail(mEmail);
                user.setAge(Integer.parseInt(mAge));
                user.setSex(mGender);
                user.setDesc(mDesc);

                //提交用户数据给bmob
                user.signUp(new SaveListener<User>() {
                    @Override
                    public void done(User myUser, BmobException e) {
                        if(e==null){
                            Toast.makeText(RegisteredActivity.this, R.string.text_registered_successful, Toast.LENGTH_SHORT).show();
                            finish();
                        }else{
                            Toast.makeText(RegisteredActivity.this, getString(R.string.text_registered_failure) + e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                break;

        }
    }

    //性别选择事件
    @OnCheckedChanged({R.id.rb_boy,R.id.rb_girl})
    public void onCheckedChanged(CompoundButton view, boolean ischanged){
        switch (view.getId()) {
            case R.id.rb_boy:
                mGender = true;
                break;
            case R.id.rb_girl:
                mGender = false;
                break;
        }
    }
}
